package com.example.muasic.activity;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.muasic.R;
import com.example.muasic.fragment.PlayQueueFragment;
import com.example.muasic.fragment.QuickControlsFragment;
import com.example.muasic.info.Info;
import com.example.muasic.permissions.PermissionHelper;
import com.example.muasic.provider.MusicSqLiteHelper;
import com.example.muasic.service.IService;
import com.example.muasic.service.MusicService;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class SearchActivity extends AppCompatActivity {
    private ImageView imageView;
    private Button btn_search;
    private EditText search;
    private ImageView back;
    private ListView listView;
    private static IService iService;
    private ServiceConnection conn;
    private MusicSqLiteHelper musicSqLiteHelper;
    private PermissionHelper permissionHelper;
    private String TAG = "PlayMusicActivity";
    private static List<String> titleList = new ArrayList<>();
    private static List<Info> musicList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);
        musicSqLiteHelper = new MusicSqLiteHelper(this,"music.db",1);
        listView=findViewById(R.id.search_list);
        search = findViewById(R.id.searchname);
        Intent intent = new Intent(this, MusicService.class);
        startService(intent);
        conn = new ServiceConnection(){
            @Override
                public void onServiceConnected(ComponentName name, IBinder service) {
                //6. 获取IBinder对象, 以此调用暴露的函数
                iService = (IService) service;
            }

            @Override
            public void onServiceDisconnected(ComponentName name) {
            }
        };

        //绑定服务

        bindService(intent, conn, BIND_AUTO_CREATE);

        imageView = findViewById(R.id.iv_back2);
        btn_search = findViewById(R.id.btn_search);
        Lode();

        back = findViewById(R.id.iv_back2);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SearchActivity.this.finish();
            }
        });

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if(iService.callgetNowMusic()<0||iService.callgetNowMusic()>=musicList.size()) {
                    PlayingActivity.playing(musicList.get(position).getData(), iService);
                    iService.callgetNowmusicList(musicList);
                    iService.callNowMusic(position);
                }else {
                    long nowMusicId=iService.callgetNowMusicId();
                    if (nowMusicId != musicList.get(position).getSongId()) {
                        /*if(iService.callgetNowMusic()!=position){*/
                        PlayingActivity.playing(musicList.get(position).getData(), iService);
                        iService.callgetNowmusicList(musicList);
                        iService.callNowMusic(position);
                    }
                }
                QuickControlsFragment.setPosition(position);
                QuickControlsFragment.setmusicList(musicList);
                PlayQueueFragment.setmusicList(musicList);
                QuickControlsFragment.updateNowplayingCard();

                Intent intent = new Intent(SearchActivity.this,
                        PlayingActivity.class);
                Bundle bundle = new Bundle();
                bundle.putInt("music_send",position);
                bundle.putSerializable("musiclist", (Serializable) musicList);
                intent.putExtras(bundle);
                startActivity(intent);

            }
        });



        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(SearchActivity.this,PlayMusicActivity.class);
                startActivity(intent);
            }
        });
        btn_search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String name = search.getText().toString();
                musicList=musicSqLiteHelper.SearchLocalmusic(musicSqLiteHelper,name);
                titleList=musicSqLiteHelper.SearchLocalmusicName(musicSqLiteHelper,name);


                //添加
                if(musicList.size()>0) {
                    Lode();
                }else{
                    Toast.makeText(SearchActivity.this, "没找到歌曲", Toast.LENGTH_LONG).show();
                }

            }
        });
    }

    private void Lode(){
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,titleList);
        listView.setAdapter(adapter);
        adapter.notifyDataSetChanged();
    }

}
